package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//change packet name to your settings
import Beans.Member;
import ServiceInterface.ServiceProxy;

/**
 * Servlet implementation class ListMembers
 */
public class ListMembers extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ServiceProxy proxy = new ServiceProxy();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListMembers() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		Member[] members;
		
		try {
			String memType = request.getParameter("mem_radios");			
			proxy.setEndpoint("http://localhost:8079/VLMS3/services/Service"); //change for your settings
			
			System.out.println("in ListMembers.java, Member Type = "+memType);
			if(memType.equalsIgnoreCase("Simple"))
				members = proxy.listSimpleMembers();
			else if(memType.equalsIgnoreCase("Premium"))
				members = proxy.listPremiumMembers();
			else
				members = proxy.listAllMembers();			
			
			if(members != null)
			{
				out.println("<h1>"+memType+" Members<h1>");
				out.println("<table><tr><th>Member#</th><th>Email</th><th>First Name</th><th>Last Name</th><th>Address</th>" +
						"<th>City</th><th>State</th><th>Zip</th><th>Type</th><th>Outstanding Movies</th><th>Fees</th></tr>");
				for(Member m : members)
				{
					out.print("<tr><td>"+m.getMemNum()+"</td>");
					out.print("<td>"+m.getEmail()+"</td>");
					out.print("<td>"+m.getFirst()+"</td>");
					out.print("<td>"+m.getLast()+"</td>");
					out.print("<td>"+m.getAddress()+"</td>");
					out.print("<td>"+m.getCity()+"</td>");
					out.print("<td>"+m.getState()+"</td>");
					out.print("<td>"+m.getZip()+"</td>");
					out.print("<td>"+m.getType()+"</td>");
					out.print("<td>"+m.getOutstandingMovies()+"</td>");
					out.print("<td>"+m.getFees()+"</td></tr>");
				}
				out.println("</table>");
			} else {
				out.println("<h1>Error Retrieving Member(s)</h1>");
				out.println("<br><br><a href=\"ListMembers.jsp\"><br>Go back to try again</a>");
			}
		} 
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
